﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using H2.NET.Web.Workflow.DwzCode;
using H2.NET.Common;
using H2.NET.Web.Workflow.Services;
using H2.NET.Business.Core;
using H2.NET.Web.Workflow.Helpers;

namespace H2.NET.Web.Management.Role
{
    public partial class SetPermission : BasePage
    {
        private PermissionService _permissionService = new PermissionService();
        private RoleService _roleService = new RoleService();

        protected void Page_Load(object sender, EventArgs e)
        {
            var action = AksRequest.GetQueryString("action");

            if (action == "set")
            {
                SetPermissions();
                return;
            }

            var id = Request.QueryString["id"];
            var role = _roleService.GetModel(Convert.ToInt32(id));
            l_rolename.Text = role.Name;
            hf_roleid.Value = id;

            InitPermissionTree();
        }

        private void SetPermissions()
        {
            var id = AksRequest.GetFormInt("hf_roleid");
            var permissions = AksRequest.GetFormString("permissions");

            _permissionService.SetPermissions(Convert.ToInt32(id), permissions.Split(',').ToArray());

            SuccessMsg("RoleIndex");
        }

        private void InitPermissionTree()
        {
            var rolePerms = _permissionService.GetByRoleId(AksRequest.GetQueryInt("id")).Select(t => t.Id).ToList();

            var allpermissions = ConvertHelper<t_mdc_Permission>.ConvertToList(_permissionService.GetList("").Tables[0]);

            string munetree = string.Empty;

            allpermissions.Where(t => t.ParentID == null).ToList().ForEach(m =>
            {
                var ck = rolePerms.Contains(m.Id) ? "checked" : "";

                munetree +=
                    string.Format(
                        "<li><a href='javascript:;' tname='permissions' tvalue='{3}' ck='{0}' title='{1}'>{2}</a>", ck,
                        m.Title, m.Name, m.Id);

                var children = allpermissions.Where(t => t.ParentID == m.Id).ToList();

                if (children.Any())
                {
                    munetree += MenuTree(munetree, allpermissions, children, rolePerms);
                }

                munetree += "</li>";
            });

            l_permission_tree.Text = munetree;
        }

        private string MenuTree(string munetree, List<t_mdc_Permission> allpermissions, List<t_mdc_Permission> permissions, List<int> rolePerms)
        {
            munetree = "<ul>";
            permissions.ForEach(m =>
            {
                var ck = rolePerms.Contains(m.Id) ? "checked" : "";
                munetree +=
                    string.Format(
                        "<li><a href='javascript:;' tname='permissions' tvalue='{3}' ck='{0}' title='{1}'>{2}</a>", ck,
                        m.Title, m.Name, m.Id);

                var children = allpermissions.Where(t => t.ParentID == m.Id).ToList();

                if (children.Any())
                {
                    munetree += MenuTree(munetree, allpermissions, children, rolePerms);
                }

                munetree += "</li>";
            });
            munetree += "</ul>";

            return munetree;
        }
    }
}